using System;
using UnityEngine;

public class AudioObserver : MonoBehaviour
{
	public static void SetVolume(float volumeRate = 1f)
	{
		if (AudioObserver._volumeRate == volumeRate)
		{
			return;
		}
		AudioObserver._volumeRate = volumeRate;
		if (AudioObserver.onChangeVolumeRate != null)
		{
			AudioObserver.onChangeVolumeRate();
		}
	}

	public static void SetSFX(bool on)
	{
		AudioObserver._sfx = on;
		if (AudioObserver.onChangeSFX != null)
		{
			AudioObserver.onChangeSFX(on);
		}
	}

	public static void SetBGM(bool on)
	{
		AudioObserver._bgm = on;
		if (AudioObserver.onChangeBGM != null)
		{
			AudioObserver.onChangeBGM(on);
		}
	}

	private void Awake()
	{
		AudioObserver.onChangeVolumeRate = (Action)Delegate.Combine(AudioObserver.onChangeVolumeRate, new Action(this.OnMsgChangeVolumeRate));
		AudioObserver.onChangeSFX = (Action<bool>)Delegate.Combine(AudioObserver.onChangeSFX, new Action<bool>(this.OnMsgChangeSFX));
		AudioObserver.onChangeBGM = (Action<bool>)Delegate.Combine(AudioObserver.onChangeBGM, new Action<bool>(this.OnMsgChangeBGM));
		if (null != base.GetComponent<AudioSource>())
		{
			this._volume = base.GetComponent<AudioSource>().volume;
		}
		this.OnMsgChangeVolumeRate();
	}

	private void OnDestroy()
	{
		AudioObserver.onChangeVolumeRate = (Action)Delegate.Remove(AudioObserver.onChangeVolumeRate, new Action(this.OnMsgChangeVolumeRate));
		AudioObserver.onChangeSFX = (Action<bool>)Delegate.Remove(AudioObserver.onChangeSFX, new Action<bool>(this.OnMsgChangeSFX));
		AudioObserver.onChangeBGM = (Action<bool>)Delegate.Remove(AudioObserver.onChangeBGM, new Action<bool>(this.OnMsgChangeBGM));
	}

	private void OnMsgChangeVolumeRate()
	{
		if (null == base.GetComponent<AudioSource>())
		{
			return;
		}
		if (this.sfx && !AudioObserver._sfx)
		{
			base.GetComponent<AudioSource>().volume = 0f;
			base.GetComponent<AudioSource>().mute = true;
		}
		else if (!this.sfx && !AudioObserver._bgm)
		{
			base.GetComponent<AudioSource>().volume = 0f;
			base.GetComponent<AudioSource>().mute = true;
		}
		else
		{
			base.GetComponent<AudioSource>().volume = this._volume * AudioObserver._volumeRate / 1f;
			base.GetComponent<AudioSource>().mute = false;
		}
	}

	private void OnMsgChangeSFX(bool on)
	{
		AudioObserver._sfx = on;
		this.OnMsgChangeVolumeRate();
	}

	private void OnMsgChangeBGM(bool on)
	{
		AudioObserver._bgm = on;
		this.OnMsgChangeVolumeRate();
	}

	public static Action onChangeVolumeRate;

	public static Action<bool> onChangeSFX;

	public static Action<bool> onChangeBGM;

	private static float _volumeRate = 1f;

	private static bool _sfx = true;

	private static bool _bgm = true;

	private float _volume = 1f;

	public bool sfx = true;
}
